package com.hxtec.polaris.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxtec.polaris.entity.*;
import com.hxtec.polaris.mapper.ShopProductBaseMapper;
import com.hxtec.polaris.mapper.ShopProductImageMapper;
import com.hxtec.polaris.mapper.ShopProductSaleAttrMapper;
import com.hxtec.polaris.mapper.ShopProductSaleAttrValueMapper;
import com.hxtec.polaris.service.ProductBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ProductBaseServiceImpl implements ProductBaseService {
    private final ShopProductBaseMapper spuMapper;

    private final ShopProductSaleAttrMapper productSaleAttrMapper;

    private final ShopProductSaleAttrValueMapper productSaleAttrValueMapper;

    private final ShopProductImageMapper spuImageMapper;

    public ProductBaseServiceImpl(ShopProductBaseMapper spuMapper, ShopProductSaleAttrMapper productSaleAttrMapper, ShopProductSaleAttrValueMapper productSaleAttrValueMapper, ShopProductImageMapper spuImageMapper) {
        this.spuMapper = spuMapper;
        this.productSaleAttrMapper = productSaleAttrMapper;
        this.productSaleAttrValueMapper = productSaleAttrValueMapper;
        this.spuImageMapper = spuImageMapper;
    }


    @Override
    public List<ShopProductSaleAttr> spuSaleAttrList(Integer spuId) {
        Example example=new Example(ShopProductSaleAttr.class);
        example.createCriteria().andEqualTo("productId",spuId);
        List<ShopProductSaleAttr> shopProductSaleAttrs = productSaleAttrMapper.selectByExample(example);
        shopProductSaleAttrs.forEach(saleAttr->{
            ShopProductSaleAttrValue saleAttrValue=new ShopProductSaleAttrValue();
            saleAttrValue.setSaleAttrId(saleAttr.getId());
            saleAttrValue.setProductId(spuId);
            List<ShopProductSaleAttrValue> saleAttrValues = productSaleAttrValueMapper.select(saleAttrValue);
            saleAttr.setSpuSaleAttrValueList(saleAttrValues);
        });
        return shopProductSaleAttrs;
    }

    @Override
    public void saveSpuInfo(ShopProductBase pmsProductInfo) {
        // 保存商品信息
        spuMapper.insertSelective(pmsProductInfo);
        // 生成商品主键
        Integer productId = pmsProductInfo.getSpu();
        //保存图片
        List<ShopProductImage> spuImageList = pmsProductInfo.getSpuImageList();
        spuImageList.forEach(image->{
            image.setProductId(productId);
            spuImageMapper.insertSelective(image);
        });
        // 保存销售属性信息
        List<ShopProductSaleAttr> spuSaleAttrList = pmsProductInfo.getSpuSaleAttrList();
        for (ShopProductSaleAttr pmsProductSaleAttr : spuSaleAttrList) {
            pmsProductSaleAttr.setProductId(productId);
            productSaleAttrMapper.insertSelective(pmsProductSaleAttr);

            // 保存销售属性值
            List<ShopProductSaleAttrValue> spuSaleAttrValueList = pmsProductSaleAttr.getSpuSaleAttrValueList();
            for (ShopProductSaleAttrValue pmsProductSaleAttrValue : spuSaleAttrValueList) {
                pmsProductSaleAttrValue.setProductId(productId);
                pmsProductSaleAttrValue.setSaleAttrId(pmsProductSaleAttr.getId());
                productSaleAttrValueMapper.insertSelective(pmsProductSaleAttrValue);
            }
        }

    }

    @Override
    public List<ShopProductImage> spuImageList(String spuId) {
        Example example=new Example(ShopProductImage.class);
        example.createCriteria().andEqualTo("productId",spuId);
        return spuImageMapper.selectByExample(example);
    }

    @Override
    public PageInfo<ShopProductBase> getSpuListInfo(Integer currentPage, Integer pageSize, String productName,String productCode,String catalogId) {
        Example example=new Example(ShopProductBase.class);
        example.createCriteria()
                .andEqualTo("categoryId",StringUtils.isNotEmpty(catalogId) ?catalogId:null)
                .andLike("name", StringUtils.isNotEmpty(productName) ?"%"+productName+"%":null)
                .andEqualTo("spu",StringUtils.isNotEmpty(productCode)?productCode:null);
        PageHelper.startPage(currentPage,pageSize);
        PageInfo<ShopProductBase> pageInfo=new PageInfo<>(spuMapper.selectByExample(example));
        return pageInfo;
    }

}
